<?php
/**
 * Created by PhpStorm.
 * User: zhang
 * Date: 2019/1/1
 * Time: 18:52
 */

namespace app\admin\controller;

use app\lib\enum\MemberLogEnum;
use think\Config;
use think\Exception;
use think\Loader;
use think\Request;
use Yurun\Util\HttpRequest;

/**
 * Class Reback
 * @package app\admin\controller
 * 退款控制器类
 */
class Reback extends Common
{
    /**
     * @return \think\response\View
     * 退款列表
     */
    public function index()
    {
        $data = [];
        //搜索
        $order_sn = input('post.order_sn');//订单编号
        $data['order_sn'] = $order_sn;
        $company_id = input('post.company_id');
        $data['company_id'] = $company_id;
        $status = input('post.status');//处理状态
        $data['refund_status'] = $status;
        //页码
        $pageNum = 1;
        if (is_numeric(input('post.pageNum'))) {
            $pageNum = input('post.pageNum');
        }
        $data['pageNum'] = $pageNum;
        //每页数量
        $numPerPage = config('num_per_page');
        $data['numPerPage'] = $numPerPage;
        $model = loader::model('Reback');
        //列表数据
        $list = $model->getList($pageNum, $numPerPage, $order_sn, $status, $company_id);
        $data['list'] = $list;
        //数据总数
        $count = $model->getCount($order_sn, $status, $company_id);
        $data['count'] = $count;
        $data['company'] = db('company')->field('id, company_name')->where('is_delete', 0)->where('is_use', 1)->select();
        return view('', $data);
    }
    /**
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 退款详情
     */
    public function detail()
    {
        $order_id = input('get.id');
        $where['order_id'] = $order_id;
        $order = db("order")->where($where)->find();
        $driving_license = db("driving_license")->where($where)->find();
        $data['order_info'] = $order;
        $data['dl_info'] = $driving_license;
        return view('', $data);
    }
    /**
     * @return \think\response\Json
     * 平台同意退款
     */
    public function agreereback()
    {
        $reback_id = input('get.id');
        try {
            $reback_model = new \app\admin\model\Reback();
            $reback_model->doagreereback($reback_id);
        } catch (Exception $e) {
            return json([
                'statusCode' => 300,
                'message' => $e->getMessage(),
            ]);
        }
        return json([
            'statusCode' => 200,
            'message' => '操作成功',
            'navTabId' => 'rebackManage'
        ]);
    }
    /**
     * @return \think\response\Json
     * 平台确认退款
     */
    public function surereback()
    {
        $reback_id = input('get.id');
        $order = db('order')->field('open_id, order_sn, user_weixin_id, total_amount')->where('order_id', $reback_id)->find();
        $open_id = $order['open_id'];
        try {
            $reback_model = new \app\admin\model\Reback();
            $reback_model->dosurereback($reback_id, $open_id);
            // +------------------------------------------------------
            // | 收银系统接口
            // +------------------------------------------------------
            $paymentOrderData = [
                'order_id' => $order['order_sn'], // 预约平台订单号
                'type'     => 2, // 检车站ID
            ];
            $this->sendOrderDataToPayment($paymentOrderData);
        } catch (Exception $e) {
            return json([
                'statusCode' => 300,
                'message' => $e->getMessage(),
            ]);
        }
        return json([
            'statusCode' => 200,
            'message' => '操作成功',
            'navTabId' => 'rebackManage'
        ]);
    }

    /**
     * @return \think\response\Json|\think\response\View
     * 平台拒绝退款
     */
    public function refusereback()
    {
        $reback_id = input('id');
        if(Request::instance()->isPost()){
            $refuse_case = input("post.refuse_case");
            $order = db('order')->field('open_id, update_time, user_weixin_id, total_amount')->where('order_id', $reback_id)->find();
            $open_id = $order['open_id'];
            try {
                $reback_model = new \app\admin\model\Reback();
                $reback_model->dorefusereback($reback_id,$open_id,$refuse_case);
                $time = time();
                if (strtotime('2020-09-09') < $order['update_time']) {
                    $memberData = [
                        'user_weixin_id' => $order['user_weixin_id'],
                        'number'         => $order['total_amount'],
                        'type'           => MemberLogEnum::INCREASE,
                        'get_from'       => MemberLogEnum::FROM_CANCEL_REBACK,
                        'create_time'    => $time,
                        'update_time'    => $time,
                    ];
                    db('member_log')->insert($memberData);
                    // 增加积分
                    db('user_weixin')->where('id', '=', $order['user_weixin_id'])->setInc('member', $order['total_amount']);
                }
            } catch (Exception $e) {
                return json([
                    'statusCode' => 300,
                    'message' => $e->getMessage(),
                ]);
            }
            return json([
                'statusCode' => 200,
                'message' => '成功',
                'navTabId' => 'rebackManage',
                'callbackType' => 'closeCurrent',
            ]);
        }
        return view('refuse_case',['reback_id'=>$reback_id]);
    }



    /**
     * @Desc 向收银系统发送订单数据
     *
     * @param array $data 要发送的数据
     *
     * @return mixed
     */
    private function sendOrderDataToPayment($data)
    {
        // $url = Config::get('edit_order_url'); // 生产环境接口
        $url = Config::get('test_edit_order_url'); // 开发环境接口
        return (new HttpRequest())->post($url, $data)->json(true);
    }
}